<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2015-2016 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionAppApi\Controller;

use AuctionCore\Other\Address;
use AuctionCore\System\AuctionException;
use AuctionCore\Other\Chip as Chip;
use AuctionCore\Common\AntNest;
use AuctionCore\System\SystemConf;
/**
 * 服务器系统时间
 * @author Xmh
 */
class SystemController extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->Chip = new Chip();
    }

    public function index() {

    }

    public function get_system_time() {
//        echo $this->second_time();
        $time = $this->microtime_time();
        $this->api_200($time);
    }

    /**
     * 精确到秒
     * return int
     */
    private function second_time() {
        $second_time = time() . '000'; //php时间戳转java时间戳
        return $second_time;
    }

    /**
     * 精确到毫秒
     * @return int
     */
    private function microtime_time() {
        list($sec, $micro) = explode(" ", microtime());
        return ceil(((float) $sec + (float) $micro) * 1000);
    }

    /**
     * 获取基本配置信息
     */
    public function get_basic_config() {
        $emission[] = array_flip(C('INT_EMISSION_STD'));
        $emission_info = $this->getArr($emission);
        $price[] = array_flip(C('APP_PRICE_RANGE'));
        $price_info = $this->getArr($price);
        $age[] = array_flip(C('APP_AGE_RANGE'));
        $age_info = $this->getArr($age);
        $plate = C('PLATE_PREFIX');
        $plate_word = C('PLATE_WORD_PREFIX');
        $location_area= array();
        for($i=0;$i<count($plate);$i++){
            $location_area[$i]['name'] = $plate[$i+1];
            $location_area[$i]['value'] = $i+1;
            $location_area[$i]['items'] = $plate_word[$i+1];
        }
        $com_state = C('APP_COM_STATE');
        $car_level = C('APP_CAR_LEVEL');
        // $loan_state[] = array_flip(C('LOAN_STATE'));
        $loan_state[] = array_flip(C('LOAN_STATE'));
        $loan_state_info = $this->getArr($loan_state);
        //$branch_company[] = array_flip(C("APP_BRANCH_COMPANY_CITY_LIST"));
        $city= Address::get_apply_city();
        $citys = array('全国'=>'-1,全国');
        foreach($city as $k=>$v){
            $citys[$v['name']]=  $k.','.$v['short_name'];
        }
        $branch_company[] = $citys;

        $branch_company_info = $this->getArr($branch_company);
        $state_level = array();
        foreach ($com_state as $ks => $cs) {
            if ($ks != '-1') {
                foreach ($car_level as $kl => $cl) {
                    if ($kl != '-1') {
                        $state_level[$ks . $kl] = $cs . $cl;
                    }
                }
            }
        }
        $state_level_arr[] = $state_level;
        $state_level_info = $this->getArr($state_level_arr);
        $state_level_all = array(
            '0' => array(
                '全部' => '-1'
            )
        );
        $state_level_final = array_merge($state_level_all, $state_level_info);
        $repair_price = SystemConf::getConfByName("REPAIR_SINGL_PRICE");
        $repair_single_price = $repair_price['value']; //维保查询单价
        $params = array(
            'emission' => $emission_info,
            'prices_interval' => $price_info,
            'year_interval' => $age_info,
            'condition_level' => $state_level_final,
            'branch_company'=> $branch_company_info,
            'reg_area'=>$location_area,
            'tel' => C('APP_TEL'),
            'loan_state'=>$loan_state_info,
            'fee_per_search'=>$repair_single_price
        );
        $this->api_200($params);
    }
    /**
     * 获取通用碎片接口
     */
    public function get_chip() {
        $json_params = I('post.params');//'[{"name":"little_knowledge","modify_time":"1461636831"},{"name":"https_certificate","modify_time":"1461636832"}]';
        if (!$this->is_json($json_params)) {
            AuctionException::make_throw('1200002');
        }
        $arr_params = json_decode($json_params, true);
        $chip_arr = array();
        foreach ($arr_params as $ak => $av) {
            $chip_arr[$ak] = $this->Chip->get_chip_info_byname($av['name']);
        }
        $chip_data = array();
        foreach ($chip_arr as $ck => $cv) {
            if (!empty($cv) && $arr_params[$ck]['modify_time'] != strtotime($cv['updatetime'])) {
                $chip_data[$ck]['name'] = $cv['name'];
                $chip_data[$ck]['content'] = $cv['content'];
                $chip_data[$ck]['modify_time'] = strtotime($cv['updatetime']);
            } else {
                $chip_data[$ck] = null;
            }
        }
        $this->api_200($chip_data);
    }

    /**
     * 判断json格式
     * @param type $json
     * @return type
     */
    private function is_json($json) {
        json_decode($json);
        return (json_last_error() == JSON_ERROR_NONE);
    }

    /**
     * 获取基本配置数组
     * @param type $arr
     * @return type
     */
    private function getArr($arr) {
        $arr_info = array();
        $i = 0;
        foreach ($arr as $ve) {
            foreach ($ve as $kv => $vv) {
                $arr_info[$i][$kv] = $vv;
                $i++;
            }
        }
        return $arr_info;
    }


    /**
     * @throws 获取ant公钥
     */
    public function getKey($type=1){
        $ant = new AntNest();
        $info = $ant->get_rsakey($type);
        if($info['code'] === '000000'){
            if($type==1){
                $keys = explode('-----',base64_decode($info['data']['public_key']));
                echo json_encode(array('code'=>200,'data'=>$keys[2]));exit;
            }

            return base64_decode($info['data']['private_key']);
        }else{
            trace(var_export($info,true),'','DEBUG',true);
        }
    }

    /**
     * 获取短信验证码
     */
    public function getSms(){
        $ant = new AntNest();
        $option['mobile'] = I('post.mobile');
        $option['number'] = I('post.number');
        if($option['number']==C("ANT_SMS_NUMBER.regist")){
            $is_reg = $ant->verify_mobile(array('mobile'=>$option['mobile']));
            if($is_reg['code']=='000000'){
                exit(json_encode(array('code'=>500,'msg'=>'手机号已被注册')));
            }
        }elseif($option['number']==C("ANT_SMS_NUMBER.retrieve_pwd")){
            $is_reg = $ant->verify_mobile(array('mobile'=>$option['mobile']));
            if($is_reg['code']=='131105'){
                AuctionException::make_throw('200041');
            }
        }
        $state = $ant->get_sms($option);
        if($state['code']==='000000'){
            exit(json_encode(array('msg' =>'验证码已发送，请注意查收', 'code' => 200)));
        }
        exit(json_encode(array('msg' =>$state['msg'], 'code' => 500)));
    }

    /**
     * 获取地址配置信息
     */
    public function getCitys(){
        $city = Address::get_apply_city();
        foreach($city as $key=>$v){
            $arr = array();
            $arr['city_code'] = $key;
            $arr['city_name'] = $v['name'];
            $citys[] = $arr;
        }
        if($citys){echo json_encode(array('code'=>200,'data'=>$citys));exit;}
        echo json_encode(array('code'=>500,'data'=>'','msg'=>'查询出错'));exit;
    }

}
